using System;
using System.Reflection;
using SQLRunnerLib.Util;
using SQLRunnerLib.Exceptions;
using SQLRunnerLib.Services;
using SQLServices.Log;

namespace SQLRunnerCommand
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	class MainClass
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static int Main(string[] args)
		{
			foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies()) {
			    if (assembly.FullName != null)
			        if (assembly.FullName.StartsWith("SQLRunnerCommand")) {
			            Console.WriteLine(string.Format("SQL Server Runner - [{0}]",
			                                            assembly.GetName().Version));
			            break;
			        }
			}
            try
            {
			    Console.WriteLine(@"http://www.codeplex.com/sqlrunner");
                LineParser parser = new LineParser(args);
                LineController controller = new LineController();
                int returnValue = controller.Start(parser);
                if ((parser.ErrorLevel & ErrorIndicator.AppFailure) == ErrorIndicator.AppFailure)
                {
                    return returnValue;
                }
                return 0;
            }
            catch (ValidationException ex)
            {
                ServiceContainer.Instance.Logger.LogMessage(ex.Message, MsgLevel.Error);
                return 1;
            }
        }
	}
}
